import {
	getUserInfo
} from '@/api/user/userInfo.js'
import {
	Logout
} from '@/api/login.js'

export default {
	methods: {
		// 设置用户凭证
		async setToken(e) {
			try {
				if (e) {
					uni.setStorageSync('token', e)
				}

				let token = uni.getStorageSync('token') || null
				if (token === null) {
					return;
				}
				// this.$store.commit('SET_TOKEN', token)

				// let res = await getUserInfo()
				// if (res.data.code === 200) {
				// 	let data = res.data.data
				// 	let userInfo = {}
				// 	this.$store.commit('SET_USER_INFO', userInfo)
				// } else {
				// 	this.removeToken(false)
				// }
			} catch (e) {
				this.removeToken(false)
			}
		},
		// 删除用户凭证
		async removeToken(logout = true, e) {
			try {
				if (e) {
					await Logout(e)
				}

				// let userInfo = {}
				// this.$store.commit('SET_TOKEN', null)
				// this.$store.commit('SET_USER_INFO', userInfo)
				uni.removeStorageSync('token')

				// 使用$nextTick确保DOM更新完再跳转，否则会跳转到一个空白页面
				this.$nextTick(() => {
					if (logout) {
						uni.switchTab({
							url: '/pages/user/user',
							success: () => {
								uni.$u.toast("登出成功")
							}
						})
					} else {
						uni.switchTab({
							url: '/pages/user/user',
							success: () => {
								uni.$u.toast("提示：您的登录已过期")
							}
						})
					}
				})
			} catch (e) {
				console.log(e)
			}
		},
	}
}